/*
 * Copyright (C) 2020-present, kinbug All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitee.kinbug.sniper.indicator;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.gitee.kinbug.sniper.common.entity.Bar;
import com.tictactec.ta.lib.Core;
import com.tictactec.ta.lib.MInteger;
import com.tictactec.ta.lib.RetCode;

import lombok.extern.slf4j.Slf4j;

/**
 * -描述
 * @ClassName: ADXIndicator
 * @author kinbug
 * @date 2020-12-30
 */
@Slf4j
public class ADXIndicator {

	public static void getValue(List<Bar> barList, final int period, final List<BigDecimal> callback) {
		callback.clear();
		double[] inHigh = new double[barList.size()];
		double[] inLow = new double[barList.size()];
		double[] inClose = new double[barList.size()];
		double[] outReal = new double[barList.size()];
		MInteger outBegIdx = new MInteger();
		MInteger outNBElement = new MInteger();
		for (int i = 0; i < barList.size(); i++) {
			inHigh[i] = barList.get(i).getHigh().doubleValue();
			inLow[i] = barList.get(i).getLow().doubleValue();
			inClose[i] = barList.get(i).getClose().doubleValue();
		}
		Core core = new Core();
		
		RetCode retCode = core.adx(0, barList.size()-1, inHigh, inLow, inClose, period,outBegIdx, outNBElement, outReal);
		if (retCode == RetCode.Success) {
        	for (int i = 0; i < barList.size()-period; i++) {
        		if (outReal[i]>0) {
        			callback.add(new BigDecimal(outReal[i]).setScale(2, RoundingMode.DOWN));
				}
            }
		}else {
			log.error("计算ADX异常");
		}
	}
}
